﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Cors;
using Brc_PaaS.Api.Common;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Core.Redis;
using Brc_PaaS.Common.Model.Redis;
using System.Collections.Generic;
using StackExchange.Redis;
using Brc_PaaS.Bll.System;

namespace Brc_PaaS.Api.Controllers.Redis
{
    /// <summary>
    /// 操作redis有序集合控制器
    /// </summary>
    [Route("api/redis/[controller]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "Redis")]
    public class RedisSetController : RedisBaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="redis"></param>
        /// <param name="system"></param>
        public RedisSetController(IConfiguration configuration, Brc_PaaS.Bll.Redis.IRedis redis, ISystem system) : base(configuration, redis, system)
        {

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<BaseResponseModel> Post(RedisSortSetModel model)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            if (SystemInfo != null && BusinessRedisServerEntity != null && model != null)
            {

                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.IsSuccess = redisManager.SortedSetAdd(model.Key, model.Value, model.Score);
                }
            }
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult<BaseResponseModel> Delete(RedisSortSetModel model)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            if (SystemInfo != null && BusinessRedisServerEntity != null && model != null)
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.IsSuccess = redisManager.SortedSetRemove(model.Key, model.Value);
                }
            }
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<ResponseModel<List<string>>> Get(RedisSortSetModel model)
        {
            ResponseModel<List<string>> result = new ResponseModel<List<string>>() { IsSuccess = false, Msg = string.Empty, Data = new List<string>() };
            if (SystemInfo != null && BusinessRedisServerEntity != null && model != null)
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.Data = redisManager.SortedSetRangeByRank<string>(model.Key, model.Page, model.Limit, model.IsDesc);
                }
            }
            result.IsSuccess = true;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }
    }
}